package com.yanqu.road.server.manger.config;

import com.yanqu.road.entity.enums.eTaskType;
import com.yanqu.road.logic.bussiness.config.TaskBussiness;
import com.yanqu.road.entity.task.TaskConditionInfo;
import com.yanqu.road.entity.task.TaskInfo;
import com.yanqu.road.pb.task.TaskProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.ClientProtocol;

import java.util.*;

public class TaskMgr extends TempMgr {

	private static Map<Integer, TaskInfo> taskMap;

	private static Map<Integer, TaskConditionInfo> taskConditionMap;

	private static List<Integer> mainTaskList;

	/**
	 * key taskType
	 */
	private static Map<Integer, List<Integer>> objectTaskIdMap = new HashMap<>();
	private static Set<Integer> objectTaskTypeSet = new HashSet<>();

	static {
		objectTaskTypeSet.add(eTaskType.TargetObjectTask.getValue());
	}


	private static TaskProto.TaskConfigSyncMsg taskConfigSyncMsg = TaskProto.TaskConfigSyncMsg.newBuilder().build();

	@Override
	public boolean init() throws Exception {
		return reload();
	}

	@Override
	public boolean stop() {
		return false;
	}

	@Override
	public boolean save() {
		return false;
	}

	@Override
	public boolean reloadConfig() {
		Map<Integer, TaskInfo> tempTaskMap = TaskBussiness.getTaskInfoMap();
		tempTaskMap = filterOldTask(tempTaskMap);
		Map<Integer, TaskConditionInfo> tempTaskConditionInfoMap = TaskBussiness.getTaskConditionInfoMap();
		tempTaskConditionInfoMap = filterOldTaskCondition(tempTaskConditionInfoMap, tempTaskMap);
		Map<Integer, List<Integer>> tempObjectTaskIdMap = initObjectTaskIdMap(tempTaskMap, tempTaskConditionInfoMap);

		List<Integer> tempMainTaskList = initMainTaskList(tempTaskMap);

		taskMap = tempTaskMap;
		objectTaskIdMap = tempObjectTaskIdMap;
		taskConditionMap = tempTaskConditionInfoMap;
		mainTaskList = tempMainTaskList;

		parseTaskSyncMsg();
		return true;
	}

	@Override
	public boolean reloadData() throws Exception {
		return true;
	}

	private static List<Integer> initMainTaskList(Map<Integer, TaskInfo> tempTaskMap) {
		List<Integer> dataList = new ArrayList<>();
		TaskInfo nowTaskInfo = null;
		for (TaskInfo taskInfo : tempTaskMap.values()) {
			if (taskInfo.getType() == eTaskType.Main.getValue() && taskInfo.getPreTaskId() == 0) {
				nowTaskInfo = taskInfo;
				break;
			}
		}
		dataList.add(nowTaskInfo.getTaskId());

		// 找主线链
		while (nowTaskInfo.getNextTaskId() > 0) {
			dataList.add(nowTaskInfo.getNextTaskId());
			nowTaskInfo = tempTaskMap.get(nowTaskInfo.getNextTaskId());
		}
		return dataList;
	}

	public static Map<Integer, TaskInfo> getTaskMap() {
		return taskMap;
	}

	public static TaskInfo getTaskInfoById(int taskId) {
		return taskMap.get(taskId);
	}

	public static Map<Integer, TaskConditionInfo> getTaskConditionMap() {
		return taskConditionMap;
	}

	public static TaskConditionInfo getTaskConditionById(int taskId) {
		return taskConditionMap.get(taskId);
	}

	/**
	 * 获取主线任务下标定位
	 */
	public static int getMainTaskIndex(int taskId) {
		return mainTaskList.indexOf(taskId);
	}

	/**
	 * taskType==22的任务,额外同步给客户端
	 */
	public static void parseTaskSyncMsg() {
		TaskProto.TaskConfigSyncMsg.Builder builder = TaskProto.TaskConfigSyncMsg.newBuilder();
		for (TaskInfo taskInfo : taskMap.values()) {
			if (taskInfo.getType() != eTaskType.WeekendSystem.getValue()) {
				continue;
			}
			TaskProto.TaskConfigTemp.Builder taskBuilder = TaskProto.TaskConfigTemp.newBuilder();
			taskBuilder.setId(taskInfo.getTaskId());
			taskBuilder.setType(taskInfo.getType());
			taskBuilder.setIntegral(taskInfo.getIntegral());
			taskBuilder.setLittleType(taskInfo.getLittleType());
			taskBuilder.setPreTaskId(taskInfo.getPreTaskId());
			taskBuilder.setNextTaskId(taskInfo.getNextTaskId());
			taskBuilder.setRewards(taskInfo.getRewardStr());
			taskBuilder.setSort(taskInfo.getSort());

			builder.addTaskData(taskBuilder);

			TaskConditionInfo taskCondition = getTaskConditionById(taskInfo.getTaskId());
			if (taskCondition != null) {
				TaskProto.TaskConditionTemp.Builder conditionBuilder = TaskProto.TaskConditionTemp.newBuilder();
				conditionBuilder.setId(taskCondition.getTaskId());
				conditionBuilder.setCompleteType(taskCondition.getType());
				conditionBuilder.setCompleteParams(taskCondition.getCompleteParams());
				conditionBuilder.setSelectable(taskCondition.isCanSelect());
				conditionBuilder.setSatisfyType(taskCondition.getSatisfyType());
				builder.addTaskConditionData(conditionBuilder);
			}
		}
		synchronized (taskConfigSyncMsg) {
			taskConfigSyncMsg = builder.build();
		}

		syncOnLineUser();
	}

	public static TaskProto.TaskConfigSyncMsg.Builder getTaskConfigSyncMsg() {
		synchronized (taskConfigSyncMsg) {
			return taskConfigSyncMsg.toBuilder();
		}
	}

	public static void syncOnLineUser() {
		TaskProto.TaskConfigSyncMsg.Builder builder = getTaskConfigSyncMsg();
		List<GamePlayer> allOnlinePlayer = GamePlayerMgr.getAllOnlinePlayer();
		for (GamePlayer player : allOnlinePlayer) {
			player.sendPacket(ClientProtocol.U_TASK_CONFIG_SEND, builder);
		}
	}

	/**
	 * 过滤掉不该存在的旧任务
	 *
	 * @param taskInfoMap
	 * @return
	 */
	private static Map<Integer, TaskInfo> filterOldTask(Map<Integer, TaskInfo> taskInfoMap) {
		Map<Integer, TaskInfo> tempMap = new HashMap<>();
		for (TaskInfo taskInfo : taskInfoMap.values()) {
			if (OldTaskChangeMgr.isNeedTransformTask(taskInfo.getType())) {
				continue;
			}
			tempMap.put(taskInfo.getTaskId(), taskInfo);
		}
		return tempMap;
	}

	/**
	 * 过滤掉不该存在的任务condition
	 *
	 * @param taskConditionInfoMap
	 * @return
	 */
	private static Map<Integer, TaskConditionInfo> filterOldTaskCondition(Map<Integer, TaskConditionInfo> taskConditionInfoMap, Map<Integer, TaskInfo> taskInfoMap) {
		Map<Integer, TaskConditionInfo> tempMap = new HashMap<>();
		for (TaskConditionInfo taskConditionInfo : taskConditionInfoMap.values()) {
			if (!taskInfoMap.containsKey(taskConditionInfo.getTaskId())) {
				continue;
			}
			tempMap.put(taskConditionInfo.getTaskId(), taskConditionInfo);
		}
		return tempMap;
	}

	private static Map<Integer, List<Integer>> initObjectTaskIdMap(Map<Integer, TaskInfo> taskInfoMap, Map<Integer, TaskConditionInfo> tempTaskConditionInfoMap) {
		Map<Integer, List<Integer>> tempMap = new HashMap<>();
		for (TaskInfo taskInfo : taskInfoMap.values()) {
			if (!isObjectTask(taskInfo.getType())) {
				continue;
			}
			TaskConditionInfo taskConditionInfo = tempTaskConditionInfoMap.get(taskInfo.getTaskId());
			if (taskConditionInfo == null) {
				getLogger().error("task有配置task_condition没配置,taskId={}", taskInfo.getTaskId());
				continue;
			}
			List<Integer> list = tempMap.get(taskConditionInfo.getType());
			if (list == null) {
				list = new ArrayList<>();
				tempMap.put(taskConditionInfo.getType(), list);
			}
			list.add(taskInfo.getTaskId());
		}
		return tempMap;
	}

	public static boolean isObjectTask(Integer taskType) {
		return objectTaskTypeSet.contains(taskType);
	}

	public static Map<Integer, List<Integer>> getObjectTaskIdMap() {
		return objectTaskIdMap;
	}

}
